package com.jic.product.controller.manage;


import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.product.common.util.BeanUtil;
import com.jic.product.entity.WarehouseProductRelation;
import com.jic.product.manage.request.WarehouseProductRelationRequest;
import com.jic.product.manage.request.WarehouseProductRelationSaveRequest;
import com.jic.product.manage.response.WarehouseProductRelationResponse;
import com.jic.product.service.WarehouseProductRelationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by Zxk on 2020/1/9 11:04
 * 仓库商品
 */
@RestController
@RequestMapping("/warehouseProductRelation")
@Api(value = "仓库商品", description = "仓库商品", tags = "仓库商品")
public class WarehouseProductRelationController {

    @Autowired
    private WarehouseProductRelationService warehouseProductRelationService;


    /*
     * 分页查询
     * */
    @ApiOperation(value = "获取所有商品计量单位信息")
    @ResponseBody
    @PostMapping(value = "/queryPage")
    public RestResult<PageResult<WarehouseProductRelationResponse>> queryPage(@RequestBody WarehouseProductRelationRequest form) {
        Page page = new Page();
        page.setLimit(form.getLimit());
        page.setStart(form.getStart());
        WarehouseProductRelation warehouseProductRelation = BeanUtil.copy(form, WarehouseProductRelation.class);
        PageResult<WarehouseProductRelationResponse> result = warehouseProductRelationService.queryPage(warehouseProductRelation, page);
        List<WarehouseProductRelationResponse> warehouseProductRelationResponseList = result.getRows().stream().map(temp -> {
            WarehouseProductRelationResponse warehouseProductRelationResponse = BeanUtil.copy(temp, WarehouseProductRelationResponse.class);
            return warehouseProductRelationResponse;
        }).filter(item -> item != null).collect(Collectors.toList());
        PageResult<WarehouseProductRelationResponse> pageResult = new PageResult<>();
        pageResult.setRows(warehouseProductRelationResponseList);
        pageResult.setTotal(result.getTotal());
        return RestResult.success(pageResult);

    }


    /*
     *  仓库商品流通修改
     *
     * */
    @ResponseBody
    @PostMapping(value = "/editWarehouseProductRelation")
    public RestResult editWarehouseProductRelation(@RequestBody WarehouseProductRelationRequest warehouseProductRelationRequest) {
        RestResult restResult = new RestResult<>();
        try {
            //修改
           long flag=warehouseProductRelationService.update(warehouseProductRelationRequest);
            return RestResult.success(true);
        } catch (Exception e) {
            e.printStackTrace();
            restResult = RestResult.error("9999", e.getLocalizedMessage());
        }
          return restResult;

    }

    /**
     * 商品进目录新增商品到商品仓库列表
     * @param warehouseProductRelationSaveRequest
     * @return
     */
    @ApiOperation(value = "保存商品和仓库")
    @PostMapping(value = "/saveWarehouseProductRelation")
    @ResponseBody
    public RestResult saveWarehouseProductRelation(@RequestBody WarehouseProductRelationSaveRequest warehouseProductRelationSaveRequest) {
        warehouseProductRelationService.saveWarehouseProductRelation(warehouseProductRelationSaveRequest);
        return RestResult.success(true);
    }

}
